﻿
using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.WebApp;
using Gift.Model;
using Gift.Utile;
using System.IO;


public partial class admin_card_professionAreaType : BaseAdmin
    {
       
      tbProfessionAreaType oArea ;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindProfession();
            }
        }

        void BindProfession()
        {
            if (Request.QueryString["ot"] == "ed")
            {
                oArea = BaseFactory.theProfessionAreaType.GetModel(int.Parse(Request.QueryString["id"]));
                this.txtProfessionName.Value = oArea.Name;
                if (oArea.IsEnable)
                {
                    this.rdIsEnable1.Checked = true;
                }
                else
                {
                    this.rdIsEnable0.Checked = true;
                }

                if (!string.IsNullOrEmpty(oArea.photoUrl))
                {
                    this.imgLogo.Src = oArea.photoUrl;
                     this.imgLogo.Style.Add("display", "block");
                }
                else
                {
                    this.imgLogo.Style.Add("display", "none");
                }

                this.txtaProfessionDescription.Value = oArea.description;

                this.txtSortOrder.Value = oArea.sortOrder.ToString() ;
            }
        }


   

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            
              if (Request.QueryString["ot"] == "ed")
              {
                  oArea = BaseFactory.theProfessionAreaType.GetModel(int.Parse(Request.QueryString["id"]));
                  oArea.Name = this.txtProfessionName.Value.Trim();
                  oArea.IsEnable = MathLibrary.ConvertNumToBool(base.Request.Form["IsEnable"]);
                  oArea.sortOrder = Convert.ToInt32(this.txtSortOrder.Value);
                  oArea.description = this.txtaProfessionDescription.Value.Trim();
                  if (this.fileProfessionLogo.PostedFile.FileName != "")
                  {
                      this.oArea.photoUrl = "/cn/logo/profession_logo/" + DateTime.Now.ToString("yyyyMMddHHssmmffffff") + Path.GetExtension(this.fileProfessionLogo.PostedFile.FileName);
                  }

                  if (BaseFactory.theProfessionAreaType.Update(oArea))
                  {
                      if (this.fileProfessionLogo.PostedFile.FileName != "")
                      {
                          if ( !string.IsNullOrEmpty(oArea.photoUrl) && File.Exists(base.Server.MapPath(oArea.photoUrl)))
                          {
                              File.Delete(base.Server.MapPath(oArea.photoUrl));
                          }
                          this.fileProfessionLogo.PostedFile.SaveAs(base.Server.MapPath(this.oArea.photoUrl));
                      }
                      base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");openNewTab('品牌列表', '/admin/goods/brandList.aspx');</script>");
                  }
                  else
                  {
                      base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
                  }
                  Response.Write("<script> alert('修改成功!');window.location.href='professionAreaTypeList.aspx';</script>");
              }
              else
              {
                  oArea = new tbProfessionAreaType();
                  oArea.Name = this.txtProfessionName.Value.Trim();
                  oArea.IsEnable = MathLibrary.ConvertNumToBool(base.Request.Form["IsEnable"]);
                  oArea.sortOrder = Convert.ToInt32(this.txtSortOrder.Value);
                  oArea.description = this.txtaProfessionDescription.Value.Trim();
                  oArea.creater = int.Parse(base.Administrator.AdminUserId);
                  oArea.createTime = DateTime.Now;
                  if (this.fileProfessionLogo.PostedFile.FileName != "")
                  {
                      this.oArea.photoUrl = "/cn/logo/profession_logo/" + DateTime.Now.ToString("yyyyMMddHHssmmffffff") + Path.GetExtension(this.fileProfessionLogo.PostedFile.FileName);
                      this.fileProfessionLogo.PostedFile.SaveAs(base.Server.MapPath(this.oArea.photoUrl));
                  }
                  BaseFactory.theProfessionAreaType.Add(oArea);
                  Response.Write("<script> alert('添加成功!');window.location.href='professionAreaTypeList.aspx';</script>");
              }
     

        }
}
    
